package server

import (
	"crypto/md5"
	"fmt"
	"time"
)

// AdminLogin 管理员登录
func AdminLogin(adminId int64) (map[string]interface{}, error) {
	list := make(map[string]interface{})
	list["admin_id"] = adminId
	//获取用户习惯
	//userPropertySql, args := sql_aid.PgTable(model.TableByUserSign).Field("is_admin").Where("user_id = ?", userId).ToSql()
	//userProperty, err := sql_aid.DataToMap(model.PgSql.Query(userPropertySql, args...))
	//if err != nil || len(userProperty) == 0 {
	//	gogo_log.Error("server_UserLogin", "用户登录", err.Error())
	//	return nil, errors.New("用户不存在1")
	//}
	//检查店铺
	//shopPropertySql, args := sql_aid.PgTable(model.TableByShop).Where("user_id = ?", userId).Where("status = ?", model.ShopByStatusNormal).ToSql()
	//shopProperty, err := sql_aid.DataToMap(model.PgSql.Query(shopPropertySql, args...))
	//if err != nil || len(shopProperty) == 0 {
	//	userProperty[0]["shop_id"] = int64(0)
	//} else {
	//	userProperty[0]["shop_id"] = shopProperty[0]["id"]
	//}
	//list["property"] = userProperty[0]
	//生成token信息
	tokenToStringOne := fmt.Sprintf("%v", adminId) + time.Now().Format("2006-05-04 15:02:01")
	tokenToMd5One := fmt.Sprintf("%x", md5.Sum([]byte(tokenToStringOne)))
	tokenToStringTwo := tokenToMd5One + time.Now().Format("2006-05-04 15:02:01")
	tokenToMd5Two := fmt.Sprintf("%x", md5.Sum([]byte(tokenToStringTwo)))
	token := fmt.Sprintf("%v-%v", adminId, tokenToMd5Two)
	//
	tokenStruct := AdminToken{ //token保存信息
		Token:   token,
		AdminId:  adminId,

	}
	if err := tokenStruct.AddToken(); err != nil {
		return nil, err
	}
	list["token"] = token

	return list, nil
}




